import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { PTRComponent, ToptipsComponent } from 'ngx-weui';
import { Observable } from 'rxjs/Observable';
import { HttpService } from '../../service/http.service';

@Component({
  selector: 'app-hosplist',
  templateUrl: './hosplist.component.html',
  styleUrls: ['./hosplist.component.css']
})

export class HosplistComponent implements OnInit {

  hosplist: any;
  hosplisturl = 'hisinfo!getDepartmentList';
  type = 'loading';
  first = false;

  @ViewChild('toptips') toptips: ToptipsComponent;
  constructor(private listdata: HttpService,
    private router: Router
  ) {
  }
  //组件加载事件
  ngOnInit() {
    this.getHospData();
    setTimeout(() => this.first = true, 300);
  }
  // 获取医院列表方法
  getHospData() {
    this.listdata.httpPost(this.hosplisturl, {}).subscribe(res => {
      this.hosplist = res;
      console.log('接口名:'+this.hosplisturl, this.hosplist);
    });
  }

  /*顶部提示*/
  onShow(type: 'warn' | 'info' | 'primary') {
    this.toptips.type = type;
    switch (type) {
      case 'warn':
        this.toptips.text = ' 刷新失败! ';
        break;
      case 'primary':
        this.toptips.text = '刷新成功！ ';
        break;
      case 'info':
        this.toptips.text = ' Thanks for coming! ';
        break;
    }
    this.toptips.onShow();
  }

  /*配合刷新组件方法*/
  onRefresh(ptr: PTRComponent) {
    Observable.timer(800).subscribe(() => {
      this.ngOnInit();
      this.onShow('primary');
      ptr.setFinished('上次刷新：' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds());
    });
  }

  /*跳转到下一个页面，携带id*/
  goIndex(index: number) {
    this.router.navigate(['/hospinfo/hospindex', index]);
  }
}
